package com.wechat.service;

import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.wechat.model.UserMessage;
import com.wechat.thread.MyThread;
import com.wechat.util.MyUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class WechatService {
	public String getAccessToken() throws Exception {
		MyUtil myUtil = MyUtil.getMyUtil();
		String sendGet = myUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token",
				"grant_type=client_credential&appid=wxbc1597774dcfa755&secret=" + "d0fc73ef2deb8882f817ff0927dcd7ad");
		JSONObject json = JSONObject.fromObject(sendGet);
		String accessToken = (String) json.get("access_token");
		if (accessToken == null || accessToken.equals("")) {
			accessToken = (String) json.get("errmsg");
		}
		// long expiresIn = (long) json.get("expires_in");
		return accessToken;
	}

	/**
	 * 文本消息处理
	 * 
	 * @return
	 * @dagewang 2017年11月17日
	 */
	public String textHandle(Map<String, String> map) throws Exception {
		String fromUserName = map.get("FromUserName");
		String message = map.get("Content");
		String result = "您好，您可以回复\"功能\"两个字获取本公众号所提供的服务和使用方法。";
		switch (message) {
		case "功能":
			result = "功能一:查询快递,回复\ncx@快递公司拼音@单号,例如\"cx@zhongtong@456789123\"即可查询快递详细信息 。\r\n"
					+ "功能二:发送您的位置,我们会为您推荐附近的美食信息。\r\n功能三:回复\"跑腿\" 可使用跑腿服务。";
			break;
		case "跑腿":
			result="http://111.231.190.67/TaskController/allTask.action";
		default:
			if (message.indexOf("cx@") >= 0 || message.indexOf("Cx@") >= 0) {
				result = this.query(message, fromUserName);
			}
			break;
		}
		UserMessage ue = new UserMessage();
		ue.setCdate(new Date());
		ue.setMessage(message+ "----" + result);
		ue.setUsername(fromUserName);
		MyThread.userMessage.add(ue);
		return result;
	}

	/**
	 * 快递查询
	 * 
	 * @dagewang 2017年11月23日
	 */
	public String query(String num, String username) throws Exception {
		String[] split = num.split("@");
		boolean b = false;
		if (split.length != 3) {
			b = true;
		} else if (!split[1].matches("[A-Za-z]{0,20}")) {
			b = true;
		} else if (!split[2].matches("[\\w]{8,30}")) {
			b = true;
		}
		if (b) {
			return "对不起,请您输入正确的值,例如 \"cx@shentong@456432432432\"!";
		}
		MyUtil myUtil = MyUtil.getMyUtil();
		String sign = "146acb5e818448b7829a404fcea7dc2a";
		String url = "http://route.showapi.com/64-19";
		String sendGet = myUtil.sendGet(url, "showapi_appid=50584&" + "showapi_sign=" + sign + "&"
				+ "showapi_timestamp=" + new Date().getTime() + "&" + "com=" + split[1] + "&" + "nu=" + split[2] + "");
		JSONObject fromObject = JSONObject.fromObject(sendGet);
		// 解析调用状态
		String analysisState = myUtil.analysisState(fromObject.getInt("showapi_res_code"));
		if (analysisState != null) {
			return analysisState;
		}
		StringBuffer content = new StringBuffer();
		JSONObject fromObject2 = JSONObject.fromObject(fromObject.get("showapi_res_body"));
		int statenum = Integer.valueOf(fromObject2.getString("status"));
		if (statenum > 1) {
			JSONArray jsonArray = JSONArray.fromObject(fromObject2.getString("data"));
			content.append(fromObject2.get("expTextName") + "\n");
			content.append("电话:" + fromObject2.get("tel") + "\n");
			String state = "查询异常";
			switch (statenum) {
			case 1:
				state = "暂无记录";
				break;
			case 2:
				state = "正在途中";
				break;
			case 3:
				state = "派送中";
				break;
			case 4:
				state = "已签收";
				break;
			case 5:
				state = "用户拒签";
				break;
			case 8:
				state = "超时单";
				break;
			case 6:
				state = "疑难件";
			default:
				break;
			}
			content.append("订单状态:" + state + "\n");
			int size = jsonArray.size();
			for (int i = 1; i < size; i++) {
				JSONObject j = jsonArray.getJSONObject(size - i);
				content.append(j.get("time") + "\n");
				content.append(j.get("context") + "\n");
			}

		} else {
			content.append("您的单号异常,查询失败!");
		}
		return content.toString();
	}

}
